package com.ethink.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ethink.order.api.enums.TocSaleImportJobLogStatusEnum;
import com.ethink.order.api.info.TocSaleImportJobLogInfo;
import com.ethink.order.api.request.TocSaleImportJobLogAddRequest;
import com.ethink.order.common.entity.TocSaleImportJobLog;
import com.ethink.order.common.mapper.TocSaleImportJobLogMapper;
import com.ethink.order.service.TocSaleImportJobLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 销售单批量导入job处理结果记录表 Service接口实现
 *
 * @author default
 * @date 2022-12-20
 */
@Slf4j
@Service
public class TocSaleImportJobLogServiceImpl implements TocSaleImportJobLogService {

    @Autowired
    private TocSaleImportJobLogMapper tocSaleImportJobLogMapper;


    @Override
    public Integer add(TocSaleImportJobLogAddRequest addRequest) {
        TocSaleImportJobLog tocSaleImportJobLog = BeanUtil.copyProperties(addRequest, TocSaleImportJobLog.class);
        return tocSaleImportJobLogMapper.insert(tocSaleImportJobLog);
    }

    @Override
    public List<TocSaleImportJobLogInfo> queryLogByLogId(Long logId) {
        return tocSaleImportJobLogMapper.queryLogByLogId(logId);
    }

    @Override
    public Integer processTocSaleImportJobLog(Long id) {
        return tocSaleImportJobLogMapper.update(null, new LambdaUpdateWrapper<TocSaleImportJobLog>()
                .eq(TocSaleImportJobLog::getId, id)
                .set(TocSaleImportJobLog::getStatus, TocSaleImportJobLogStatusEnum.S2.getIntValue())
                .set(TocSaleImportJobLog::getIsSuccess, true));
    }

    @Override
    public List<TocSaleImportJobLogInfo> queryFailureLogs(Long id) {
        return tocSaleImportJobLogMapper.queryFailureLogs(id);
    }

    @Override
    public Integer updateByShopNoAndLogId(TocSaleImportJobLogAddRequest updateRequest) {
        return tocSaleImportJobLogMapper.update(null, new LambdaUpdateWrapper<TocSaleImportJobLog>()
                .eq(TocSaleImportJobLog::getLogId, updateRequest.getLogId())
                .eq(TocSaleImportJobLog::getShopOrderNo, updateRequest.getShopOrderNo())
                .eq(TocSaleImportJobLog::getIsDeleted, false)
                .set(TocSaleImportJobLog::getStatus, updateRequest.getStatus())
                .set(TocSaleImportJobLog::getIsSuccess, updateRequest.getIsSuccess())
                .set(TocSaleImportJobLog::getFailReason, updateRequest.getFailReason()));
    }

}
